package org.example.sep_24;

public class Topic_3074 {
    public static void main(String[] args) {
        int[] apple = {2,11,12,17,15,5,6,14,5,16,5,19,7,12,20,14};
        int[] capacity = {10,1,20,5,9,6,5,12,11,11,14,14,1,16,17,14,7,1,12};
        System.out.println(new Topic_3074().minimumBoxes(apple, capacity));
    }

    public int minimumBoxes(int[] apple, int[] capacity) {
        int apppleSum = 0;
        for (int n : apple) {
            apppleSum += n;
        }
        int capacityCount = 0;
        int[] candidateV = new int[51];
        for (int n : capacity) {
            candidateV[n]++;
        }
        int V = 0;
        for (int i = 50; i > 0; i--) {
            if (apppleSum <=0){
                break;
            }
            if (candidateV[i] > 0) {
                if (apppleSum - candidateV[i] * i >= 0) {
                    capacityCount += candidateV[i];
                    apppleSum -= candidateV[i] * i;
                } else {
                    for (int j = 0; j < candidateV[i]; j++) {
                        if (apppleSum - i <= 0) {
                            capacityCount++;
                            apppleSum -= i;
                            break;
                        } else {
                            capacityCount++;
                            apppleSum -= i;
                        }
                    }
                }
            }
        }
        return capacityCount;
    }
}
